package com.dunshan.data.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author dunshan
 * @program: mall-swarm-new
 * @description: 日志获取file
 * @date 2021-11-14 15:40:11
 */
@Component
public class ContextFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String header = request.getHeader(DataSourceNames.DUNSHAN);
        AppContext appContext = new AppContext();
        if (StringUtils.isNotEmpty(header)) {
            appContext.setFlag(header);
            AppContext.setContext(appContext);
        } else {
            AppContext.setContext(appContext);
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
        AppContext.removeContext();
        Filter.super.destroy();

    }

}
